A r t i c l e s
Navigation

Note: This site is
a bit older, personal views
may have changed.

M a i n P a g e

D i r e c t o r y

Redirecting STDOUT To Text File


Redirecting standard out on the command line to a text file is not so hard if you know about the OUTPUT global variable which you can take control of.
program project1;

{$mode objfpc}{$H+}

var
  RedOutFile: text;

procedure RedirectSTDOUT(FileName: string);
begin
//  close(OUTPUT); //closes STDOUT, actually doesn't seem to be needed. 
  Assign(OUTPUT, FileName); // now assign a file to STDOUT (redirect)
  Rewrite(OUTPUT); // open file for writing
end;

procedure RestoreSTDOUT;
begin
  close(output); // close file
  Assign(output,''); // restore STDOUT mode
  rewrite(output); // open STDOUT for writing
end;

begin
  WriteLn('This text is written to console');
  RedirectStdOut('Error.txt');
  writeln('This text is written to Error.txt');

  // want to restore std out?
  RestoreStdOut;
  WriteLn('Back to console again');
  readln;
end.


About
This site is about programming and other things.
_ _ _